 
import wx

""" this version use class 'BufferedPaintDC' and 'BufferedDC' """

class MainFrame(wx.Frame):
    
    def __init__(self):
        wx.Frame.__init__(self, None, -1, "画图板 - 芒果布丁", (0, 0), (800, 500))
        self.Center(wx.BOTH) # 窗口居中显示
        # self.panel = wx.Panel(self, )
        # sizer = wx.BoxSizer(wx.VERTICAL)
        # self.panel.SetSizer(sizer)

 

        self.x1, self.x2, self.y1, self.y2 = 0, 0, 0, 0
        self.st = 'line' 
        self.pos = (0,0)
        self.pen = wx.Pen("green", 1, wx.SOLID)
        self.brush = wx.Brush('', wx.TRANSPARENT)  #透明填充
        self.shapes = []
        
        self.InitBuffer()
                          
        self.SetBackgroundColour("black")
        self.b1 = wx.Button(self , -1, label="矩形", pos=(10, 10), size=(50, 30))
        self.b2 = wx.Button(self , -1, label="圆形", pos=(10, 50), size=(50, 30))
        self.b3 = wx.Button(self , -1, label="直线", pos=(10, 90), size=(50, 30))
        
        self.Bind(wx.EVT_BUTTON, self.ToRect, self.b1)
        self.Bind(wx.EVT_BUTTON, self.ToOval, self.b2)
        self.Bind(wx.EVT_BUTTON, self.ToLine, self.b3)
        
        self.Bind(wx.EVT_LEFT_DOWN, self.OnLeftDown)
        self.Bind(wx.EVT_LEFT_UP, self.OnLeftUp)
        self.Bind(wx.EVT_PAINT, self.OnPaint)
        self.Bind(wx.EVT_SIZE, self.OnSize)

    def InitBuffer(self):
        size = self.GetClientSize()
        self.buffer = wx.Bitmap(size.width, size.height)

        print(f"InitBuffer........... {self.buffer}  {self.buffer.GetSize()}")
        
    def ToRect(self, event):
        self.st = 'rect'
        
    def ToOval(self, event):
        self.st = 'oval'
        
    def ToLine(self, event):
        self.st = 'line'
        
    def OnLeftDown(self, event):
        # self.p2 = event.GetPositionTuple()
        self.p1 = event.GetPosition()
        print(self.p1)
        self.x1, self.y1 = self.p1
        
    def OnLeftUp(self, event):
        # self.p2 = event.GetPositionTuple()
        self.p2 = event.GetPosition()
        print(self.p2)
        self.shapes.append((self.st, *self.p1 , *self.p2))
        print("shaeos   ",self.shapes)
        self.Draw( )
        
    def OnPaint(self, event):
        dc = wx.BufferedPaintDC(self, self.buffer) # 处理一个paint（描绘）请求
    
    def Draw(self):
        dc = wx.BufferedDC(wx.ClientDC(self), self.buffer)
        dc.SetPen(self.pen)
        dc.SetBackground(wx.Brush(self.GetBackgroundColour()))
        dc.SetBrush(self.brush)
        dc.Clear()

        print(self.st ,self.x1,self.y1,self.x2,self.y2, self.shapes )
        for st, x1,y1,x2,y2  in self.shapes:
            if st == 'line':
                dc.DrawLine(x1, y1, x2, y2)
            elif st == 'oval':
                dc.DrawEllipse(x1, y1, x2-x1, y2-y1)
            elif st == 'rect':
                dc.DrawRectangle(x1, y1, x2-x1, y2-y1)  
                
        # self.draw_axes()


    def draw_axes(self):
        # 获取窗口的大小
        width, height = self.panel.GetSize()

        # 绘制x轴
        self.dc.DrawLine(30, height - 30, width - 30, height - 30)

        # 绘制y轴
        self.dc.DrawLine(30, 30, 30, height - 30)

        # 绘制刻度
        for i in range(1, 11):
            x = int(30 + i * (width - 60) / 10)
            y = height - 30
            self.dc.DrawLine(x, y - 5, x, y + 5)

            x = 30
            y = int(height - 30 - i * (height - 60) / 10)
            self.dc.DrawLine(x - 5, y, x + 5, y)

        # 绘制标签
        self.dc.DrawText("时间", 40, height - 15)
        self.dc.DrawText("幅度", 15, 40)


    def OnSize(self, event):
        # 在尺寸变化时，获取新的尺寸并更新布局
        size = event.GetSize()
        self.panel.SetSize(size)
        print(f"  ==== {size}")
        event.Skip()
        
if __name__ =='__main__':
    app = wx.App()
    frame = MainFrame()
    frame.Show()
    app.MainLoop()
    

 
#  import wx

# class MyPanel(wx.Panel):
#     def __init__(self, parent):
#         super().__init__(parent)
#         self.Bind(wx.EVT_PAINT, self.on_paint)

#     def on_paint(self, event):
#         dc = wx.BufferedPaintDC(self)
#         w, h = self.GetSize()
#         dc.SetSize(w, h)
#         # 在此处绘制图形
#         #...
#         # 完成后，将缓冲区的内容绘制到屏幕上
#         dc.Blit(0, 0, w, h, wx.ClientDC(self), 0, 0)

# app = wx.App()
# frame = wx.Frame(None, title="Buffered PaintDC Example")
# panel = MyPanel(frame)
# frame.Show()
# app.MainLoop()


#  4     wxClientDC dc(this);
#  5     // 设置一个矩形区域
#  6     dc.SetClippingRegion(wxPoint(0,0),wxSize(200,20));
#  7     // 清除之前绘制的文字
#  8     dc.Clear();
#  9     // 设置文字的前景色
# 10     dc.SetTextForeground(wxColour(255,255,0));
# 11     // 设置文字的字体
# 12     dc.SetFont(wxFont(10, wxFONTFAMILY_SWISS, wxFONTSTYLE_NORMAL, wxFONTWEIGHT_NORMAL, false, wxT("宋体")));
# 13     wxPoint pt = event.GetPosition();
# 14     dc.DrawText(wxString::Format(wxT("当前坐标x-%d y-%d"), pt.x, pt.y),wxPoint(0,0));
# 15     dc.SetFont(wxNullFont);
# 16     // 销毁之前设置的区域
# 17     dc.DestroyClippingRegion();

# 1     wxClientDC dc(this);
# 2     dc.SetMapMode(wxMM_TEXT);
# 3     dc.SetUserScale(1.0,1.0);